package com.zlx.guguanjia.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zlx.guguanjia.entity.SysRole;
import com.zlx.guguanjia.mapper.SysRoleMapper;
import com.zlx.guguanjia.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author 周罗校
 * @Description
 * @ClassName SysRoleServiceImpl
 * @date 2020.11.03 18:59:44
 * 该版权归周罗校所有，最终解释权以官方为准
 */
@Service
@Transactional
public class SysRoleServiceImpl extends BaseServiceImpl<SysRole,Long> implements SysRoleService {
    @Autowired
    private SysRoleMapper mapper;

    @Override
    public PageInfo selectPage(int start, int pageSize, Map<String, Object> condition){
        PageHelper.startPage(start,pageSize);
        return new PageInfo<SysRole>(mapper.selectPage(condition));
    }

    @Override
    public int deleteBatch(long rid, long[] ids) {
        return mapper.deleteBatch(rid,ids);
    }

    @Override
    public int insertBatch(long rid, List<Long> cids) {
        return mapper.insertBatch(rid,cids);
    }
    /**
     * @Author 周罗校
     * @Description 1.更新role信息
     *              2.更新sys_role_resource 信息
     *              3.更新sys_role_office信息
     * @Param [ params{
     *           role:角色信息；
     *          resources：新权限信息；_resources:旧权限的信息
     *          offices： 新授权的公司信息；_offices: 旧的
     *          }]
     * @return int
     * @Date 2020.11.05 16:54:29
     **/
    @Override
    public int updateByPrimaryKeySelective(Map<String, Object> params) {
        //1.更新role信息
        //根据key获取角色
        Object roleObj = params.get("role");
        SysRole role=null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            //map获取的数值不能强转为对象类型，需要json转换一下
            String s = objectMapper.writeValueAsString(roleObj);
            role = objectMapper.readValue(s, SysRole.class);
            role.setUpdateDate(new Date());
            //更新用户信息
            super.updateByPrimaryKeySelective(role);
        } catch (Exception e) {
            throw new RuntimeException("转换Json异常");
        }
        //旧权限的id
        List<Integer> resources = null;
        List<Integer> _resources = null;
        if (params.containsKey("resources")){
            resources= (List<Integer>) params.get("resources");
        }
        if (params.containsKey("_resources")){
            _resources= (List<Integer>) params.get("_resources");
        }
        //比较新旧的权限id
        if (resources.size()==_resources.size()&&resources.containsAll(_resources)){
            //新旧的权限一致,不用更新
            System.out.println("数据一致，无需更新");
        }else {

//            System.out.println("数据不一致，需要更新");
            //先角色id删除所有该角色的所有权限
            mapper.deleteByRid(role.getId());
            //再添加新的所有的角色权限信息
            if (resources.size()>0){
                //再添加新的所有的角色权限信息
                mapper.insertBatchResources(role.getId(),resources);
            }
        }
        List<Integer> offices = null;
        List<Integer> _offices = null;
        //公司授权管理
        if (params.containsKey("offices")){
            offices= (List<Integer>) params.get("offices");
        }
        if (params.containsKey("_offices")){
            _offices= (List<Integer>) params.get("_offices");
        }
        //比较新旧的公司id
        if (offices.size()==_offices.size()&&offices.containsAll(_offices)){
            //新旧的权限一致,不用更新
            System.out.println("数据一致，无需更新");
        }else {
            if (_offices!=null){
                mapper.deleteOfficeByRid(role.getId());
            }
            if(offices.size()>0&&offices!=null){//原来有授权，现在取消授权的情况，只删除，不插入
                mapper.insertRoleOffices(role.getId(),offices);
            }
        }

        return 1;
    }
}
